<?php
/**
 * 购物车
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class Cart extends Model
{
    //set talbename
    protected $table = 'cart';
    //set primary key
    protected $pk = 'cart_id';

    /**
     * @desc 添加购物车
     * @return false|int
     */
    public function addCart($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    /**
     * @desc 根据条件获取购物车商品
     * @param $conds $fields
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCartByCond($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    public function getCount($conds = [], $field = 'goods_id')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return Db::table($this->table)->where($arrCond)->count($field);
    }

    /**
     * @desc 获取购物车
     * @param $user_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCart($user_id)
    {
        $conds = array(
            'user_id' => $user_id,
        );
        $field = '*';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    /**
     * @desc 获取购物车商品
     * @param $user_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCartGoods($user_id, $goods_id, $goods_spec_id)
    {
        $conds = array(
            'user_id' => $user_id,
            'goods_id' => $goods_id,
            'goods_spec_id' => $goods_spec_id,
        );
        $field = '*';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    /**
     * @desc 编辑购物车
     * @param $cart_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function editCart($user_id, $goods_id, $goods_spec_id, $goods_number)
    {
        $conds = array(
            'user_id' => $user_id,
            'goods_id' => $goods_id,
            'goods_spec_id' => $goods_spec_id,
        );
        $field = array(
            'goods_number' => $goods_number,
        );
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @desc 删除购物车
     * @param $cart_id
     * @return int
     */
    public function deleteCart($user_id, $goods_id, $goods_spec_id)
    {
        $conds = array(
            'user_id' => $user_id,
            'goods_id' => ['in', $goods_id],
            'goods_spec_id' => $goods_spec_id,
        );
        return Db::table($this->table)->where($conds)->delete();
    }
}
